package com.nalai.market.v1.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nalai.market.v1.mapper.GoodsMapper;
import com.nalai.market.v1.mapper.ItemMapper;
import com.nalai.market.v1.mapper.OrderMapper;
import com.nalai.market.v1.mapper.UserMapper;
import com.nalai.market.v1.model.Goods;
import com.nalai.market.v1.model.Item;
import com.nalai.market.v1.model.Order;
import com.nalai.market.v1.model.User;
import com.nalai.market.v1.service.IOrderService;
import com.nalai.market.v1.vo.OrderVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author com.nalai
 * @since 2021-08-23
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    @Autowired
    private ItemMapper itemMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<OrderVo> getOrderIspay() {
        List<Order> orders = orderMapper.getIsPayOrder();
        return getOrderVos(orders);
    }

    @Override
    public List<OrderVo> getOrderIsconfirm() {
        List<Order> orders = orderMapper.getIsConfirmOrder();
        return getOrderVos(orders);
    }

    @Override
    public List<OrderVo> getOrderIssend() {
        List<Order> orders = orderMapper.getIsSendOrder();
        return getOrderVos(orders);
    }

    @Override
    public List<OrderVo> getOrderIsreceive() {
        List<Order> orders = orderMapper.getIsReceiveOrder();
        return getOrderVos(orders);
    }

    @Override
    public List<OrderVo> getOrderIsfinish() {
        List<Order> orders = orderMapper.getIsFinishOrder();
        return getOrderVos(orders);
    }

    @Override
    public void operationConfirm(int id) {
        orderMapper.operationConfirm(id);
    }

    @Override
    public void operationSend(int id) {
        orderMapper.operationSend(id);
    }

    @Override
    public void operationReceive(int id) {
        orderMapper.operationReceive(id);
    }

    @Override
    public void operationFinish(int id) {
        orderMapper.operationFinish(id);
    }

    @Override
    public void operationDelete(int id) {
        orderMapper.operationDelete(id);
    }

    private List<OrderVo> getOrderVos( List<Order> orders){
        List<OrderVo> orderVos = new ArrayList<>();
        for (Order o :orders) {
            Item item = itemMapper.selectById(o.getItemId());
            User user = userMapper.selectById(item.getUserId());
            Goods goods = goodsMapper.selectById(item.getGoodsId());
            OrderVo orderVo = new OrderVo();
            BeanUtils.copyProperties(o,orderVo);
            orderVo.setUserNickname(user.getNickname());
            orderVo.setGoodsName(goods.getIntro());
            orderVo.setGoodsPrice(goods.getCurrentPrice());
            orderVos.add(orderVo);
        }
        return orderVos;
    }

}
